﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SL.WorkfowDesigner.Designer
{
    public class DesignerPanManager
    {
        private bool _enablePanZoom = false;

        public bool EnablePanZoom
        {
            get { return _enablePanZoom; }
            set { _enablePanZoom = value; }
        }

        private Grid panGrid;
        private ScrollViewer panScrollViewer;
        private ScaleTransform panGridScaleTransform;
        private Point? lastDragPoint;

        public DesignerPanManager(Grid _panGrid, ScrollViewer _panScrollViewer, ScaleTransform _panGridScaleTransform, bool enablePanZoom)
        {
            this._enablePanZoom = enablePanZoom;
            this.panGrid = _panGrid;
            this.panScrollViewer = _panScrollViewer;
            this.panGridScaleTransform = _panGridScaleTransform;

            this.panGrid.MouseLeftButtonUp += ScrollViewer_OnMouseLeftButtonUp;
            this.panGrid.MouseMove += ScrollViewer_OnMouseMove;
            this.panGrid.MouseLeftButtonDown += ScrollViewer_OnMouseLeftButtonDown;
 
        }


        private void ScrollViewer_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_enablePanZoom)
            {
                var mousePos = e.GetPosition(panGrid);
                if (mousePos.X <= panScrollViewer.ViewportWidth && mousePos.Y < panScrollViewer.ViewportHeight) //make sure we still can use the scrollbars
                {
                    panGrid.Cursor = Cursors.Hand;
                    lastDragPoint = mousePos;
                    panGrid.CaptureMouse();
                }
            }
        }

        private void ScrollViewer_OnMouseMove(object sender, MouseEventArgs e)
        {
            if (_enablePanZoom)
            {
                if (lastDragPoint.HasValue)
                {
                    Point posNow = e.GetPosition(panGrid);

                    double dX = posNow.X - lastDragPoint.Value.X;
                    double dY = posNow.Y - lastDragPoint.Value.Y;

                    lastDragPoint = posNow;

                    panScrollViewer.ScrollToHorizontalOffset(panScrollViewer.HorizontalOffset - panGridScaleTransform.ScaleX * dX);
                    panScrollViewer.ScrollToVerticalOffset(panScrollViewer.VerticalOffset - panGridScaleTransform.ScaleY * dY);
                }
            }
        }

        private void ScrollViewer_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_enablePanZoom)
            {
                panGrid.Cursor = Cursors.Arrow;
                panGrid.ReleaseMouseCapture();
                lastDragPoint = null;
            }
        }
    }
}
